package com.websuites.utils;

import java.io.UnsupportedEncodingException;

@SuppressWarnings("unused")
public class Hex
{
	private static final char[] HEX_CHARS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
	private static final int COLS_PER_ROW = 8;
	private static final int BYTES_PER_ROW = 16;
	
	public static String asHex(byte[] paramArrayOfByte, int paramInt)
	{
		return asHex(paramArrayOfByte, paramInt, true);
	}
	
	public static String asUnicode(String paramString)
	{
		String str = "";
		for (int i = 0; i < paramString.length(); i++) 
		{
			str = str + "\\u" + asHex(paramString.charAt(i) >> '\b') + asHex(paramString.charAt(i));
		}
		return str;
	}
	
	public static String asUnicode(char paramChar) 
	{
		StringBuffer localStringBuffer = new StringBuffer();
		localStringBuffer.append("\\u");
		localStringBuffer.append(asHex(paramChar >> '\b') + asHex(paramChar));
		return localStringBuffer.toString();
	}
	
	public static String asHex(String paramString1, String paramString2)
		throws UnsupportedEncodingException
	{
		byte[] arrayOfByte = paramString1.getBytes(paramString2);
		return asHex(arrayOfByte);
	}
	
	public static byte[] asHexBytes(byte[] paramArrayOfByte, int paramInt, boolean paramBoolean)
	{
		int i = Math.min(paramInt, paramArrayOfByte.length);
		
		int j = 0;
		byte[] arrayOfByte;
		if (paramBoolean) 
		{
			arrayOfByte = new byte[i * 2 + 2];
			arrayOfByte[0] = 48;
			arrayOfByte[1] = 120;
			j += 2;
		} 
		else 
		{
			arrayOfByte = new byte[i * 2];
		}
		
		for (int k = 0; k < i; k++) 
		{
			arrayOfByte[(j++)] = (byte)HEX_CHARS[((paramArrayOfByte[k] & 0xF0) >> 4)];
			arrayOfByte[(j++)] = (byte)HEX_CHARS[((paramArrayOfByte[k] & 0xF) >> 0)];
		}
		return arrayOfByte;
	}
	
	@SuppressWarnings("deprecation")
	public static String asHex(byte[] paramArrayOfByte, int paramInt, boolean paramBoolean)
	{
		return new String(asHexBytes(paramArrayOfByte, paramInt, paramBoolean), 0);
	}
	
	public static byte[] fromHexString(byte[] paramArrayOfByte, int paramInt)
	{
		int i = 0;
		
		if ((paramArrayOfByte[0] == 48) && ((paramArrayOfByte[1] == 120) || (paramArrayOfByte[1] == 88))) 
		{
			i += 2;
			paramInt -= 2;
		}
		
		int j = paramInt / 2;
		byte[] arrayOfByte = new byte[j];
		
		for (int k = 0; k < j; k++) 
		{
			arrayOfByte[k] = (byte)(hexValueOf(paramArrayOfByte[(i++)]) << 4 | hexValueOf(paramArrayOfByte[(i++)]));
		}
		return arrayOfByte;
	}
	
	public static byte[] fromHexString(String paramString)
	{
		byte[] arrayOfByte;
		try
		{
			arrayOfByte = paramString.getBytes("US-ASCII");
		}
		catch (UnsupportedEncodingException localUnsupportedEncodingException) 
		{
			arrayOfByte = new byte[paramString.length()];
		} 
		for (int i = 0; i < arrayOfByte.length; i++) 
		{
			arrayOfByte[i] = (byte)paramString.charAt(i);
		}
	
		return fromHexString(arrayOfByte, arrayOfByte.length);
	}
	
	public static String asHex(int paramInt)
	{
		char[] arrayOfChar = new char[2];
		arrayOfChar[0] = HEX_CHARS[((paramInt & 0xF0) >> 4)];
		arrayOfChar[1] = HEX_CHARS[((paramInt & 0xF) >> 0)];
		return new String(arrayOfChar);
	}
	
	public static String asHex(byte[] paramArrayOfByte)
	{
		return asHex(paramArrayOfByte, paramArrayOfByte.length);
	}
	
	public static int hexValueOf(int paramInt) 
	{
		if ((paramInt >= 48) && (paramInt <= 57)) return paramInt - 48;
		if ((paramInt >= 97) && (paramInt <= 102)) return paramInt - 97 + 10;
		if ((paramInt >= 65) && (paramInt <= 70)) return paramInt - 65 + 10;
		return 0;
	}
	
	public static String dump(byte[] paramArrayOfByte)
	{
		if (paramArrayOfByte == null) 
		{
			return "" + paramArrayOfByte;
		}
		return dump(paramArrayOfByte, 0, paramArrayOfByte.length);
	}
	
	public static String dump(byte[] paramArrayOfByte, int paramInt1, int paramInt2)
	{
		if (paramInt1 < 0) paramInt1 = 0;
		
		int i = Math.min(paramArrayOfByte.length, paramInt1 + paramInt2);
		
		int j = paramInt1 & 0xFFFFFFF0;
		
		int k = i + 15 & 0xFFFFFFF0;
		
		StringBuffer localStringBuffer = new StringBuffer();
		
		int m = j;
		
		for (int n = j; n < k; n++) 
		{
			if (n % 16 == 0) 
			{
				lineLabel(localStringBuffer, n);
				m = n;
			}
			if ((n < paramInt1) || (n >= i)) 
			{
				localStringBuffer.append("  "); 
			}
			else 
			{
				localStringBuffer.append(asHex(paramArrayOfByte[n]));
			}
			if (n % 2 == 1) 
			{
				localStringBuffer.append(' ');
			}
			
			if (n % 16 == 15)
			{
				localStringBuffer.append("  ");
				for (int i1 = m; i1 < m + 16; i1++) 
				{
					if ((i1 < paramInt1) || (i1 >= i)) 
					{
						localStringBuffer.append(' '); 
					}
					else
					{
						localStringBuffer.append(toPrint(paramArrayOfByte[i1]));
					}
				}
				localStringBuffer.append('\n');
			}
		}
		return localStringBuffer.toString();
	}
	
	public static final boolean isHexChar(int paramInt)
	{
		switch (paramInt) 
		{ 
			case 48:
			case 49:
			case 50:
			case 51:
			case 52:
			case 53:
			case 54:
			case 55:
			case 56:
			case 57:
			case 65:
			case 66:
			case 67:
			case 68:
			case 69:
			case 70:
			case 97:
			case 98:
			case 99:
			case 100:
			case 101:
			case 102:
			return true;
			case 58:
			case 59:
			case 60:
			case 61:
			case 62:
			case 63:
			case 64:
			case 71:
			case 72:
			case 73:
			case 74:
			case 75:
			case 76:
			case 77:
			case 78:
			case 79:
			case 80:
			case 81:
			case 82:
			case 83:
			case 84:
			case 85:
			case 86:
			case 87:
			case 88:
			case 89:
			case 90:
			case 91:
			case 92:
			case 93:
			case 94:
			case 95:
			case 96: 
		} 
		return false;
	}
	
	private static char toPrint(byte paramByte)
	{
		int i = paramByte;
		
		if ((i < 32) || (i > 126)) 
		{
			return '.';
		}
		return (char)i;
	}
	
	private static void lineLabel(StringBuffer paramStringBuffer, int paramInt) 
	{
		String str = new Integer(paramInt).toString();
		StringBuffer localStringBuffer;
		if (str.length() <= 5) 
		{
			localStringBuffer = new StringBuffer("    ");
			localStringBuffer.insert(5 - str.length(), str);
			localStringBuffer.setLength(5);
		} 
		else
		{
			localStringBuffer = new StringBuffer(str);
		}
		
		paramStringBuffer.append(localStringBuffer);
		paramStringBuffer.append(": ");
	}
}
